package cn.yuanqiao.archive.collection.appManager.service.impl;


import cn.yuanqiao.archive.collection.appManager.domain.YqDataCategory;
import cn.yuanqiao.archive.collection.appManager.domain.YqDataTree;
import cn.yuanqiao.archive.collection.appManager.mapper.YqDataCategoryMapper;
import cn.yuanqiao.archive.collection.appManager.mapper.YqDataTreeMapper;
import cn.yuanqiao.archive.collection.appManager.service.IYqDataTreeService;
import cn.yuanqiao.archive.collection.danganjieshou.domain.YqGcReceiveFolder;
import cn.yuanqiao.archive.collection.danganjieshou.mapper.YqGcReceiveFolderMapper;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.SnowflakeIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 资料树Service业务层处理
 *
 * @ProjectName: ruoyi-modules-system
 * @Package: cn.yuanqiao.archive.appManager.service
 * @ClassName: YqDataTreeServiceImpl
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/7/28 11:36
 * @Version: 1.0
 */
@Service
public class YqDataTreeServiceImpl implements IYqDataTreeService {
    @Autowired
    private YqDataTreeMapper yqDataTreeMapper;

    @Autowired
    private YqGcReceiveFolderMapper yqGcReceiveFolderMapper;

    @Autowired
    private YqDataCategoryMapper yqDataCategoryMapper;

    /**
     * 查询资料树
     *
     * @param ID 资料树主键
     * @return 资料树
     */
    @Override
    public YqDataTree selectYqDataTreeByID(String ID) {
        return yqDataTreeMapper.selectYqDataTreeByID(ID, new YqDataTree().getTableName());
    }

    /**
     * 查询资料树列表
     *
     * @param yqDataTree 资料树
     * @return 资料树
     */
    @Override
    public List<YqDataTree> selectYqDataTreeList(YqDataTree yqDataTree) {
        return yqDataTreeMapper.selectYqDataTreeList(yqDataTree, new YqDataTree().getTableName());
    }

    /**
     * 新增资料树
     *
     * @param yqDataTree 资料树
     * @return 结果
     */
    @Override
    public int insertYqDataTree(YqDataTree yqDataTree) {
        List<YqDataTree> yqDataTrees = yqDataTreeMapper.selectYqDataTreeByCode(yqDataTree.getDataCode(), new YqDataTree().getTableName());
        if (yqDataTrees == null || yqDataTrees.size() > 0) {
            if (yqDataTrees.stream().anyMatch(a -> !a.getID().equals(yqDataTree.getID()))) {
                throw new ServiceException("资料树的资料代码重复，请重新进行填写");
            }
        }
        yqDataTree.setID(String.valueOf(SnowflakeIdGenerator.generateId()));
        return yqDataTreeMapper.insertYqDataTree(yqDataTree, new YqDataTree().getTableName());
    }

    /**
     * 修改资料树
     *
     * @param yqDataTree 资料树
     * @return 结果
     */
    @Override
    public int updateYqDataTree(YqDataTree yqDataTree) {
        List<YqDataTree> yqDataTrees = yqDataTreeMapper.selectYqDataTreeByCode(yqDataTree.getDataCode(), new YqDataTree().getTableName());
        if (yqDataTrees == null || yqDataTrees.size() > 0) {
            if (yqDataTrees.stream().anyMatch(a -> !a.getID().equals(yqDataTree.getID()))) {
                throw new ServiceException("资料树的资料代码重复，请重新进行填写");
            }
        }
        return yqDataTreeMapper.updateYqDataTree(yqDataTree, new YqDataTree().getTableName());
    }

    /**
     * 批量删除资料树
     *
     * @param IDs 需要删除的资料树主键
     * @return 结果
     */
    @Override
    public int deleteYqDataTreeByIDs(String[] IDs) {
        //查询组卷信息
        List<YqGcReceiveFolder> folders = yqGcReceiveFolderMapper.selectYqGcReceiveFolderByTreeId(IDs[0], new YqGcReceiveFolder().getTableName());
        if (folders != null && folders.size() > 0) {
            throw new ServiceException("请先删除该资料树下的接收文件!!");
        }
        return yqDataTreeMapper.deleteYqDataTreeByIDs(IDs, new YqDataTree().getTableName());
    }

    /**
     * 删除资料树信息
     *
     * @param ID 资料树主键
     * @return 结果
     */
    @Override
    public int deleteYqDataTreeByID(String ID) {
        //查询组卷信息
        List<YqGcReceiveFolder> folders = yqGcReceiveFolderMapper.selectYqGcReceiveFolderByTreeId(ID, new YqGcReceiveFolder().getTableName());
        if (folders != null && folders.size() > 0) {
            throw new ServiceException("请先删除该资料树下的接收文件!!");
        }
        YqDataTree yqDataTree = yqDataTreeMapper.selectYqDataTreeByID(ID, new YqDataTree().getTableName());

        yqDataTreeMapper.deleteYqDataTreeBytreeCode(yqDataTree.getDataCode(), new YqDataCategory().getTableName());
        return yqDataTreeMapper.deleteYqDataTreeByID(ID, yqDataTree.getTableName());
    }
}
